package keeto.com.vn.classified.ui;

import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.network.IProtocol;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ReportViolations extends Dialog implements android.view.View.OnClickListener  {

	private Context ctx;
	private Button btnOk;
	private Button btnCancel;
	private EditText content;
	private Listenner mListener = new Listenner();
	private LinearLayout llParent;
	private String ProductID;
	private int UserID = 0;
	private String Mesage;

	public ReportViolations(Context context, String ProductID) {
		super(context);
		this.ctx = context;
		this.ProductID = ProductID;
		
	}


	protected ReportViolations(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	public ReportViolations(Context context, int theme) {
		super(context, theme);

	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.logout_dialog_layout);
		llParent = (LinearLayout)findViewById(R.id.parent_logout_dialog);
		btnOk = (Button)findViewById(R.id.btn_ok);
		btnCancel = (Button)findViewById(R.id.btn_cancel);
		content = (EditText) findViewById(R.id.tvContentDialog);
		btnOk.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		this.setCanceledOnTouchOutside(false);
		Rect displayRectangle = new Rect();
        Window window = this.getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
        llParent.setMinimumWidth((int) (displayRectangle.width()* 0.9f));
        
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_ok:
			Mesage = content.getText().toString();
			if(Mesage.length() == 0){
				Toast.makeText(ctx, ctx.getResources().getString(R.string.error_report_violations), Toast.LENGTH_SHORT).show();
			} else {
				ClassifiedCore.getInstance(ctx).TaskSendAndroidProductAbusel(mListener, ProductID, UserID, Mesage);
				this.dismiss();
			}
			break;
		case R.id.btn_cancel:
			this.dismiss();
			break;

		default:
			break;
		}
	}
	class Listenner extends ClassifiedListener{
		
		@Override
		public void finishsendAndroidProductAbusel(final int returnCode, String resultMsg) {
			super.finishsendAndroidProductAbusel(returnCode, resultMsg);
			((Activity) ctx).runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if (returnCode == IProtocol.STATUS_CODE_OK_200){
						showMessageToast(ctx.getResources().getString(R.string.text_successful));
					} else {
						showMessageToast(ctx.getResources().getString(R.string.login_fail));
					}
				}
			});
		}

	}
	
	protected void showMessageToast(final String message) {
		((Activity) ctx).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(ctx,message, Toast.LENGTH_SHORT).show();
			}

		});
	}
}
